package com.dure.hotmemory;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.dure.commonlib.util.PreferencesUtils;
import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.DaoSession;
import com.dure.hotmemory.bean.NoteDao;
import com.dure.hotmemory.bean.Planet;
import com.dure.hotmemory.drawview.UniverseView;
import com.dure.hotmemory.listeners.AdPresentListener;
import com.dure.hotmemory.listeners.ConfirmListener;
import com.dure.hotmemory.scenes.BaseScene;
import com.dure.hotmemory.scenes.MainScene;
import com.dure.hotmemory.scenes.WelcomeScene;
import com.dure.hotmemory.utils.ADUtil;
import com.dure.hotmemory.utils.DialogUtil;
import com.dure.hotmemory.utils.GDTADUtil;
import com.umeng.socialize.UMShareAPI;

public class MainActivity extends BaseActivity {
    public FrameLayout parentFL;
    private UniverseView universeView;
    public RelativeLayout adBottomRL,splashRL;
    public  DaoSession daoSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adBottomRL = (RelativeLayout) findViewById(R.id.bottom_ad_layout);
        splashRL = (RelativeLayout) findViewById(R.id.splash_ad_layout);
        parentFL = (FrameLayout) findViewById(R.id.parent_fl);
        universeView = new UniverseView(this);

        int totalStartTimes = PreferencesUtils.getInt(this, "total_start_times", 0);
        if (totalStartTimes >= 1) {
            splashRL.setVisibility(View.VISIBLE);

            GDTADUtil.showSplashAd(this, splashRL, new AdPresentListener() {
                @Override
                public void adPresent() {

                }

                @Override
                public void adDismiss() {
                    parentFL.removeAllViews();
                    parentFL.addView(universeView);

                    MainScene mainScene = new MainScene(universeView);
//                mainScene.animationFinished = true;
                    universeView.setCurrentScene(mainScene);
                }
            });
            GDTADUtil.showInterstitialAd(this);
        }else{
            parentFL.addView(universeView);

            WelcomeScene welcomeScene = new WelcomeScene(universeView);
//            parentFL.addView(welcomeScene.sceneView);
            universeView.setCurrentScene(welcomeScene);
        }

//        parentFL.addView(universeView);

        daoSession = ((MyApplication) getApplication()).getDaoSession();

        PreferencesUtils.putInt(this, "total_start_times", totalStartTimes + 1);

        checkPermission();
    }


    public void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0版本以后的要进行动态授权
            if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
                //申请WRITE_EXTERNAL_STORAGE权限
                requestPermissions(new String[]{
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.READ_PHONE_STATE},
                        1);
            }
        }else{

        }
    }



    public void moveToNewScene(final BaseScene newScene) {
        final BaseScene currentScene = universeView.getCurrentScene();

        if (currentScene.sceneView == null && newScene.sceneView == null) {
            universeView.moveToNewScene(newScene);
        } else if (currentScene.sceneView == null && newScene.sceneView != null) {
            parentFL.addView(newScene.sceneView);
            universeView.moveToNewScene(newScene);
        } else if (currentScene.sceneView != null && newScene.sceneView == null) {
            universeView.moveToNewScene(newScene);
        } else {
            parentFL.addView(newScene.sceneView);
            universeView.moveToNewScene(newScene);
        }

    }

    public void backToLastScene() {
        final BaseScene currentScene = universeView.getCurrentScene();
        final BaseScene oldScene = universeView.getOldScene();

        if (oldScene != null) {
            if (currentScene.sceneView == null && oldScene.sceneView == null) {
                universeView.backToLastScene();
            } else if (currentScene.sceneView != null && oldScene.sceneView == null) {
                universeView.backToLastScene();
            } else if (currentScene.sceneView == null && oldScene.sceneView != null) {
                universeView.backToLastScene();
            } else {
                universeView.backToLastScene();
            }

        } else {// 显示退出窗口

        }
    }

    @Override
    public void onClick(View v) {
        universeView.currentScene.viewClicked(v);
    }

    @Override
    public void onBackPressed() {
        if (universeView.sceneList.size() > 1){
            backToLastScene();
        }else if (universeView.sceneList.size() == 1) {
            MainScene mainScene = (MainScene) universeView.currentScene;

            Planet currentPlanet = mainScene.getCurrentPlanet();
            if (currentPlanet != null && currentPlanet.parent_planet != null) {
                mainScene.handleBack();
            } else {
                DialogUtil.showExitDialog(this, new ConfirmListener() {

                    @Override
                    public void confirmed(Bundle bundle) {
                        finish();
                        System.exit(0);

                    }
                });
            }

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (universeView.currentScene != null)
            universeView.currentScene.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (universeView.currentScene != null)
            universeView.currentScene.permissionGranted(requestCode);
    }

    public void showInterstitialAd(){
        if (!PreferencesUtils.getBoolean(this, "paied")){
            int totalStartTimes = PreferencesUtils.getInt(this, "total_start_times");
            if (totalStartTimes >= 1){
                GDTADUtil.showInterstitialAd(this);

            }
        }
    }

    public void hideAd() {
        adBottomRL.setVisibility(View.GONE);
    }

    public void showAd() {
        adBottomRL.setVisibility(View.GONE);
//        if (!PreferencesUtils.getBoolean(this, "paied")) {
//            if (!PreferencesUtils.getBoolean(this, "is_removed")){
//                if (adBottomRL.getChildCount() == 0)
//                    ADUtil.showBannerAd(this, adBottomRL);
//                adBottomRL.setVisibility(View.VISIBLE);
//            }
//
//        }
        if (!PreferencesUtils.getBoolean(this, "paied")){
            int totalStartTimes = PreferencesUtils.getInt(this, "total_start_times");
            if (totalStartTimes >= 0){
                if (adBottomRL.getChildCount() == 0)
                    ADUtil.showBannerAd(this, adBottomRL);
                adBottomRL.setVisibility(View.VISIBLE);
            }
        }
    }
}
